<style>
    .formdesc {
        line-height: 35px;
    }
</style>
<div class=" rc-pannel ">
    <div class="rc-pannel-body">
        <div class="layui-tab layui-tab-brief" lay-filter="tab_personal">
            <ul class="layui-tab-title" id="personer_tab_li">

                <li class="layui-this" lay-id="0">账号信息</li>
                <?php
                foreach ($infotables as $tabkey => $infotable) {
                ?>
                    <li lay-id="<?php echo $tabkey + 1 ?>"><?php echo $infotable['title'] ?></li>
                <?php
                }
                ?>

            </ul>

            <div class="layui-tab-content ">
                <div class="layui-tab-item layui-show rc-pagepadding">

                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">账号：</label>
                            <div class="layui-input-block formdesc">
                                <?php echo request()->userinfo['username']; ?>
                            </div>

                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">密码：</label>
                            <div class="layui-input-block formdesc">
                                <a href="javascript:;" id="changepwd" class="primary">修改密码</a>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">手机：</label>
                            <div class="layui-input-block formdesc">
                                <?php echo request()->userinfo['phone']; ?>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">邮箱：</label>
                            <div class="layui-input-block formdesc">
                                <?php echo request()->userinfo['email']; ?>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">积分：</label>
                            <div class="layui-input-block formdesc">
                                <?php echo request()->userinfo['integral']; ?>
                            </div>

                        </div>




                    </form>

                </div>


                <?php foreach ($infotables as $tabkey => $infotable) {


                ?>
                    <div class="layui-tab-item rc-pagepadding">
                        <?php

                        if ($infotable['multiline'] != 1) {
                        ?>
                            <form class="layui-form">
                                <?php
                                foreach ($infotable['fields'] as $field) {
                                ?>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><?php echo  $field['title'] ?></label>
                                        <div class="layui-input-block">
                                            <?php
                                            $element = $field['element'];
                                            unset($field['element']);
                                            unset($field['id']);
                                            $field['value'] =  isset($infotable['infomation'][$field['name']]) ? $infotable['infomation'][$field['name']] : '';
                                            echo element($element, ...$field) ?>

                                        </div>
                                    </div>
                                <?php
                                }
                                ?>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="submit" tablename="<?php echo $infotable['name'] ?>">修改</button>
                                        <button type="reset" class="layui-btn ">重置</button>
                                    </div>

                                </div>
                            </form>
                        <?php
                        } else {
                        ?>
                            <div class="rc-flex  toolbar rc-flex-justify-between">
                                <div class=" rc-flex rc-flex-vertical-center ">

                                </div>
                                <div>
                                    <a href="javascript:;" title="<?php echo $infotable['title'] ?>" class="addinfomation layui-btn layui-btn-primary layui-btn-sm" tablename="<?php echo $infotable['name'] ?>"><i class="iconfont">&#xe60e;</i> 添加<?php echo $infotable['title'] ?></a>

                                </div>
                            </div>


                            <table class="layui-table">
                                <colgroup>
                                    <?php
                                    foreach ($infotable['fields'] as $field) {
                                    ?>
                                        <col>
                                    <?php
                                    }
                                    ?>
                                    <col width="150">
                                </colgroup>
                                <thead>
                                    <tr>
                                        <?php
                                        foreach ($infotable['fields'] as $field) {
                                        ?>
                                            <th><?php echo $field['title']; ?></th>
                                        <?php
                                        }
                                        ?>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    foreach ($infotable['infomation'] as $infomation) {
                                    ?>
                                        <tr>
                                            <?php
                                            foreach ($infotable['fields'] as $field) {

                                                //重数据字典里获值
                                                if ($field['sourcetype'] == 1) {
                                                    if (isset($infotable['dicts'][$field['sourceval']])) {
                                                        $dictvalues = json_decode($infotable['dicts'][$field['sourceval']]['values'], true);
                                                        $dictvalues = array_column($dictvalues, null, 'realvalue');
                                            ?>
                                                        <td><?php echo isset($dictvalues[$infomation[$field['name']]]) ? $dictvalues[$infomation[$field['name']]]['displayname'] : $infomation[$field['name']]; ?></td>

                                                    <?php
                                                    } else {
                                                    ?>
                                                        <td><?php echo $infomation[$field['name']]; ?></td>
                                                    <?php
                                                    }
                                                } else {
                                                    ?>
                                                    <td><?php echo $infomation[$field['name']]; ?></td>
                                            <?php
                                                }
                                            }
                                            ?>
                                            <td>
                                                <div class="rc-opbar">
                                                    <a class="info editinfomation" title="编辑<?php echo $infotable['title'] ?>" infomationid="<?php echo $infomation['id'] ?>" tablename="<?php echo $infotable['name']; ?>"><i class="iconfont">&#xe643;</i> 编辑</a>
                                                    <a class="error deleteinfomation" infomationid="<?php echo $infomation['id'] ?>" tablename="<?php echo $infotable['name']; ?>"><i class="iconfont">&#xe607;</i> 删除 </a>


                                                </div>
                                            </td>
                                        </tr>
                                    <?php
                                    }
                                    ?>


                                </tbody>

                            </table>


                        <?php
                        }
                        ?>

                    </div>
                <?php


                } ?>

            </div>

        </div>
    </div>
</div>

<script>
    layui.use(function() {
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;


        element.tabChange('tab_personal', '<?php echo request()->get('tabid', 0) ?>');


        $('.addinfomation').click(function() {
            var title = $(this).attr('title');
            var tablename = $(this).attr('tablename');
            layer.open({
                type: 2,
                title: '添加' + title,
                shadeClose: true,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content: '/uc/Personal/addInfomation?tablename=' + tablename + '&tabid=' + $('#personer_tab_li').find('.layui-this').index()

            });
        });
        $('.editinfomation').click(function() {
            var infomationid = $(this).attr('infomationid');
            var tablename = $(this).attr('tablename');
            var title = $(this).attr('title');
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content: '/uc/Personal/editInfomation?tablename=' + tablename + '&id=' + infomationid + '&tabid=' + $('#personer_tab_li').find('.layui-this').index()

            });


        });
        $('.deleteinfomation').click(function() {
            var infomationid = $(this).attr('infomationid');
            var tablename = $(this).attr('tablename');
            var that = $(this);
            layer.confirm('确定要删除？', {
                btn: ['确定', '关闭'] //按钮
            }, function() {
                $.ajax({
                    url: '/uc/Personal/delInfomation?tablename=' + tablename + '&id=' + infomationid, //请求路径
                    type: "POST", //请求方式
                    success: function(data) {
                        if (data.code == 0) {
                            that.parent().parent().parent().remove();
                            toastr_success("删除成功", function() {

                            });
                        } else {
                            toastr_error(data.message);
                        }
                    },
                    error: function() {

                    },
                    complete: function() {
                        layer.closeAll();
                    }

                });


            }, function() {

            });

        });

        $('#changepwd').click(function() {
            layer.open({
                type: 2,
                title: '修改密码',
                shadeClose: true,
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '360px'],
                content: '/uc/Personal/changePwd'

            });
        });


        form.on('submit(submit)', function(data) {
            var tablename = $(this).attr('tablename');

            $.ajax({
                url: '/uc/Personal/editInfomation?tablename=' + tablename, //请求路径
                type: "POST", //请求方式
                data: data.field, // 默认写法
                success: function(data) {
                    if (data.code == 0) {
                        toastr_success("保存成功", function() {
                            window.location.href = '/uc/Personal/index';
                        });
                    } else {
                        toastr_error(data.message);
                    }
                },
                error: function() {

                },

            });


        });
    });
</script>